package com.ruoyi.equip.util;

import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.equip.domain.DeviceInfo;
import com.ruoyi.equip.domain.EquipProduct;
import com.ruoyi.equip.domain.cache.DeviceTagCache;
import com.ruoyi.equip.domain.cache.TagCache;
import com.ruoyi.equip.domain.vo.TagVo;
import org.springframework.util.CollectionUtils;

import javax.swing.*;
import java.util.HashMap;
import java.util.Objects;

/**
 * 设备相关缓存操作
 */
public class CacheUtils {


    public static HashMap<String,DeviceTagCache> deviceTagCacheHashMap=new HashMap<>();

    //public static HashMap<String,>

    /**
     * 分隔符
     */
    public static final String SEPARATOR = ",";


    public static void setDeviceTagCache(DeviceInfo deviceInfo) {
        DeviceTagCache deviceTagCache = new DeviceTagCache(deviceInfo.getId(), deviceInfo.getSbId(), deviceInfo.getDeviceName(), deviceInfo.getProductKey(),deviceInfo.getDeptId(),deviceInfo.getUserId(), new HashMap<>());
        setDeviceTagCache(deviceTagCache.getDeviceSbId(), deviceTagCache);
    }


    /**
     * 设置设备缓存
     *
     * @param key           参数键
     * @param deviceTagData 设备数据
     */
    public static void setDeviceTagCache(String key, DeviceTagCache deviceTagData) {
        deviceTagCacheHashMap.put(getCacheKey(key), deviceTagData);
    }


    /**
     * 更新设备缓存读写器内容
     */
    public static void updateDeviceInfo(String oldKey, String newKey, DeviceInfo deviceInfo) {
        DeviceTagCache cache = getDeviceTagCache(oldKey);
        cache.setDeviceName(deviceInfo.getDeviceName());
        // 判断sbId是否发生改变
        if (!oldKey.equals(newKey)) {
            // 删除旧key
            deleteDeviceTagCache((oldKey));
        }
        setDeviceTagCache(newKey, cache);
    }


    /**
     * 更新设备缓存标签内容
     */
    public static void updateTagInfo(String key, TagVo tagVo) {
        DeviceTagCache deviceTagCache = getDeviceTagCache((key));
        HashMap<String, TagCache> tags = deviceTagCache.getTags();
        if (CollectionUtils.isEmpty(tags)) {
            tags = new HashMap<>();
        }
        tags.put(tagVo.getSbId(), new TagCache(tagVo.getId(), tagVo.getSbId(), tagVo.getTagName(), tagVo.getProductKey(), tagVo.getMax(),tagVo.getMin(),tagVo.getType(), tagVo.getState()));
        deviceTagCache.setTags(tags);
        setDeviceTagCache((key), deviceTagCache);
    }

    /**
     * 更新设备缓存标签内容
     */
    public static void updateTagInfo(String oldKey, String newKey, TagVo tagVo) {
        if (StringUtils.isNotBlank(oldKey)) {
            if (!Objects.equals(oldKey, newKey)) {
                // 移除旧缓存
                DeviceTagCache deviceTagCache = getDeviceTagCache((oldKey));
                HashMap<String, TagCache> tags = deviceTagCache.getTags();
                if (CollectionUtils.isEmpty(tags)) {
                    return;
                }
                tags.remove(tagVo.getSbId());
                deviceTagCache.setTags(tags);
                setDeviceTagCache((oldKey), deviceTagCache);
            }
        }
        //添加新缓存
        if (StringUtils.isNotBlank(newKey)) {
            updateTagInfo(newKey, tagVo);

        }


    }

    /**
     * 删除指定标签缓存
     */
    public static void removeTagInfo(String key, String tagSbId) {
        DeviceTagCache deviceTagCache = getDeviceTagCache((key));
        HashMap<String, TagCache> tags = deviceTagCache.getTags();
        if (CollectionUtils.isEmpty(tags)) {
            return;
        }
        tags.remove(tagSbId);
        deviceTagCache.setTags(tags);
        setDeviceTagCache((key), deviceTagCache);
    }

    /**
     * 删除指定设备缓存
     */
    public static void deleteDeviceTagCache(String key) {
        deviceTagCacheHashMap.remove(getCacheKey(key));
    }

    /**
     * 获取指定设备缓存
     */
    public static DeviceTagCache getDeviceTagCache(String key) {
        return deviceTagCacheHashMap.get(getCacheKey(key));
    }

    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    public static String getCacheKey(String configKey) {
        return CacheConstants.DEVICE_TAG_KEY + configKey;
    }


    public static void clearCacheData(String sbId) {

        SpringUtils.getBean(RedisCache.class).deleteObject("tag:property:" + sbId);
    }
}
